package xyz.spoplar.learnservice;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Intent intent1;
    Intent intent2;

    MyBindService service;
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder binder) {
            //当启动源和Service成功连接之后，会自动调用的方法

           service =  ((MyBindService.MyBinder)binder).getService();

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            //当启动源与service连接意外丢失的时候会调用这个方法
            //比如当Service崩溃了或者被强行杀死了
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void doClick(View v){
        switch (v.getId()){
            case R.id.StartService:
                intent1 =  new Intent(MainActivity.this,MyStartService.class);
                startService(intent1);
                break;
            case R.id.StopService:
                stopService(intent1);
                break;
            case R.id.bindService:
                intent2 =  new Intent(MainActivity.this,MyBindService.class);
                bindService(intent2,connection, Service.BIND_AUTO_CREATE);
                break;
            case R.id.unbindService:
                unbindService(connection);
                break;
            case R.id.play:
                service.play();
                break;
            case R.id.prev:
                service.prev();
                break;
            case R.id.stop:
                service.pause();
                break;
            case R.id.next:
                service.next();
                break;
            case R.id.network:
                if(isNetworkConnected(MainActivity.this) == true){
                    Toast.makeText(MainActivity.this, "网络打开", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this, "网络断开", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        unbindService(connection);
        super.onDestroy();
    }

    public boolean isNetworkConnected(Context context){
        if(context != null){
          ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
          NetworkInfo networkInfo =  connectivityManager.getActiveNetworkInfo();
            if(networkInfo !=null){
                return networkInfo.isAvailable();
            }else{
                return false;
            }
        }
        return false;
    }

}
